package com.kcnet.qk.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtils {
    private static Context mContext;
    private static String mSdRootPath;
    private static String mSdAppcache;
    private static String CRASH_PATH;
    private static  String mSdPricPath;



    public static void initFileManage(Context context) {
        mContext = context;
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            File appDir = new File(Environment.getExternalStorageDirectory(),
                    "kcnet");
            if (!appDir.exists()) {
                appDir.mkdirs();
            }
            mSdRootPath = appDir.getPath() + File.separator;
        }
        mSdAppcache = mContext.getCacheDir().getPath() + File.separator;
    }


    public static  String getCrashPath() {
        if(CRASH_PATH ==null ){
            CRASH_PATH = Environment.getExternalStorageDirectory()+"/log/crash/";
        }
        return CRASH_PATH;

    }

    public static  String getPicutures() {
        if(mSdPricPath ==null ){
            mSdPricPath = Environment.getExternalStorageDirectory()+"/Pictures/";
        }
        return mSdPricPath;

    }



    public static String getDirectory() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return mSdRootPath;
        }
        return mSdAppcache;

    }



    public static String getAPKStorageDirectory(){
        File file = new File(getDirectory() +  "apk");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getPath() + File.separator;
    }

    public static String getLogStorageDirectory(){
        File file = new File(getDirectory() +  "log");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getPath() + File.separator;
    }
    /**
     * 复制整个文件夹内容
     *
     * @param oldPath String 原文件路径 如：c:/fqf
     * @param newPath String 复制后路径 如：f:/fqf/ff
     * @return boolean
     */
    public static boolean copyFolder(String oldPath, String newPath) {
        boolean isok = true;
        try {
            (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath
                            + "/" + (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {// 如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        } catch (Exception e) {
            isok = false;
        }
        return isok;
    }

    /**
     * 获取文件名
     *
     * @param @param  pathandname
     * @param @return
     * @return String
     * @throws
     * @Title: getFileName
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD
     * author:
     * description:
     */
    public static String getFileName(String pathandname) {

        int start = pathandname.lastIndexOf("/");
        int end = pathandname.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return null;
        }

    }

    /**
     * 获取Iamges缓存文件名
     *
     * @param @param  pathandname
     * @param @return
     * @return String
     * @throws
     * @Title: getFileName
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD
     * author:
     * description:
     */
    public static String getCacheFileName(String pathandname) {

        int start = pathandname.lastIndexOf("/");
        if (start != -1) {
            return pathandname.substring(start + 1);
        } else {
            return null;
        }

    }

    /**
     * 递归删除文件和文件夹
     *
     * @param file 要删除的根目录
     */
    public static void deleteFile(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.isDirectory()) {
            File[] childFile = file.listFiles();
            if (childFile == null || childFile.length == 0) {
                file.delete();
                return;
            }
            for (File f : childFile) {
                deleteFile(f);
            }
            file.delete();
        }
    }

    /**
     * 获取目录文件大小
     *
     * @param dir
     * @return
     */
    public static long getDirSize(File dir) {
        if (dir == null) {
            return 0;
        }
        if (!dir.isDirectory()) {
            return 0;
        }
        long dirSize = 0;
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                dirSize += file.length();
            } else if (file.isDirectory()) {
                dirSize += file.length();
                dirSize += getDirSize(file); // 递归调用继续统计
            }
        }
        return dirSize;
    }

    /**
     * 转换文件大小
     *
     * @param fileS
     * @return B/KB/MB/GB
     */
    public static String formatFileSize(long fileS) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }
}
